﻿/**
 * Copyright (c) 2008 michiyasu wada
 * http://www.seyself.com/
 * 
 * Distributed under The MIT License.
 * [http://www.opensource.org/licenses/mit-license.php]
 */

package demo.objects.items 
{
import api.amazon.AmazonAPI;
import demo.scenes.DetailPageScene;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import flash.net.URLRequest;
import jp.progression.casts.CastSprite;
import jp.progression.casts.CastButton;
import demo.buttons.ItemClipButton;
import jp.progression.commands.DoTweener;
import demo.modules.AmazonSearch;
import flash.text.TextField;
import flash.display.MovieClip;
import jp.progression.scenes.SceneId;
import com.seyself.utils.BitmapUtil;
import demo.objects.OverlayPageCast;

public class DetailCast extends OverlayPageCast
{
    private var scene:DetailPageScene;
    
    private var _itemId:String;
    public function get itemId():String
    {
        return _itemId;
    }
    
    public function get linkBtn():CastButton
    {
        return CastButton( getChildByName("_linkBtn") );
    }
    
    public function get image():MovieClip
    {
        return MovieClip( pageContents.getChildByName("_image") );
    }
    
    public function get output():TextField
    {
        return TextField(pageContents.getChildByName("_output"));
    }
    
    public function DetailCast( itemId:String, previousId:SceneId, scene:DetailPageScene ) 
    {
        super( previousId );
        this.innerWidth = 600;
        this.innerHeight = 400;
        this.scene = scene;
        _itemId = itemId;
        
        closeBtn.toolTip.text = "詳細画面を閉じる";
        linkBtn.toolTip.text = "商品ページへ移動";
        
        AmazonSearch.idSearch( _itemId , setDetailData );
    }
    
    override protected function _onCastAdded():void
    {
        super._onCastAdded();
    }
    
    override protected function _onCastRemoved():void
    {
        super._onCastRemoved();
    }
    
    
    private function setDetailData( data:XML ):void
    {
        var nsList:Array = data.namespaceDeclarations();
        var ns:Namespace = Namespace(nsList[0]);
        
        var nodes:XMLList = data.children();
        var attrs:XMLList = nodes[1].Attributes.ns::ItemAttributes.children();
        var node:XML = nodes[1];
        var len:int = attrs.length();
        var str:String = "";
        for (var i:int = 0; i < len; i++ ) {
            str += "<b>"+attrs[i].localName() + "</b> : " + attrs[i].text() +"\n";
        }
        output.htmlText = str;
        BitmapUtil.swapBitmap( output );
        
        scene.title = node.Title + "｜" + scene.progression.root.title;
        
        if( node.Image.URL.toString().length ){
            var request:URLRequest = new URLRequest( node.Image.URL );
            var loader:Loader = new Loader();
            image.addChild( loader );
            
            loader.contentLoaderInfo.addEventListener( Event.INIT , loadInitHandler );
            loader.load( request );
            
            loader.x = (160 - node.Image.Width ) / 2 >> 0;
            loader.y = (160 - node.Image.Height) / 2 >> 0;
        }
        
        linkBtn.href = node.DetailPageURL;
    }
    
    private function loadInitHandler( event:Event ):void
    {
        
    }
    
    
}

}